/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQml 2.12
import QtGraphicalEffects 1.12
import org.ukui.tablet 1.0
import "."

Item {
    id: root
    property int iconState: LauncherItem.Normal
    property string appIconSource
    property int iconWidth
    property int deleteIconWidth: 30
    property bool deleteIconVisible: false
    property int deleteIconLeftMargin
    property int deleteIconTopMargin
    property string deleteIconSource
    property string deleteIconSourceOnPressed
    //edu-screen
    property int selecticonWidth: 30 * Style.widthScaleFactor
    property int iconRadius: 10
    property int eduAppId: 0
    property int eduAppPageIndex: 0
    property bool selected: true
    property bool eduAppIconOpacityMaskVisible: false
    //tablet
    property bool disableDelete: false
    property bool tabletAppIconDropShadowVisible: false

    signal unload(var eduAppId)
    signal checked(var eduAppId)
    signal unchecked(var eduAppId)
    signal deleteIconClicked()

    state: {
        switch (iconState) {
        case LauncherItem.Normal: return "Normal";
        case LauncherItem.Editable: return "Editable";
        case LauncherItem.Selectable: return "Selectable";
        default: return "Normal"
        }
    }

    states: [
        State {
            name: "Normal"
            StateChangeScript {
                script: {
                    if (deleteiconAnimation.running)
                        deleteiconAnimation.stop();
                    if (appiconAnimation.running)
                        appiconAnimation.stop();
                    if (selecticonAnimation.running)
                        selecticonAnimation.stop();
                    iconBack.rotation = 0
                    deleteIcon.scale = 0;
                    selecticon.scale = 0;
                    if(modelData.type === LauncherItem.AddIcon) {
                        appIcon.width = (iconWidth/2)
                        appIcon.height = (iconWidth/2)
                        mask.visible = true
                    }
                }
            }
        },
        State {
            name: "Editable"
            StateChangeScript {
                script: {
                    deleteiconAnimation.start();
                    appiconAnimation.start();
                }
            }
        },
        State {
            name: "Selectable"
            StateChangeScript {
                script: {
                    selecticonAnimation.start(); }
            }
        }
    ]    
    width: iconWidth
    height: iconWidth

    Rectangle {
        id: iconBack
        width: iconWidth
        height: iconWidth
        color: "transparent"
        Rectangle {
            id: mask
            width: iconWidth
            height: iconWidth
            radius: iconRadius
            visible: false
            anchors.centerIn: parent
        }
        Image {
            id: appIcon
            source: root.appIconSource
            anchors.centerIn: parent
            width: iconWidth
            height: iconWidth
            sourceSize: Qt.size(iconWidth, iconWidth)
            MouseArea {
                anchors.fill: parent
                visible: iconState === LauncherItem.Selectable
                onClicked: {
                    selecticon.selected = !selecticon.selected
                    modelManager.setItemSelectState(eduAppId, eduAppPageIndex, selecticon.selected)
                }
            }
        }
        DropShadow {
            anchors.fill: appIcon
            horizontalOffset: iconWidth/32
            radius: iconWidth/32
            samples: iconWidth/32 * 2 + 1
            color: "#000000"
            opacity: 0.15
            source: appIcon
            visible: tabletAppIconDropShadowVisible
        }
        OpacityMask {
            id: appIconOpacityMask
            anchors.fill: appIcon
            source: appIcon
            maskSource: mask
            visible: eduAppIconOpacityMaskVisible
        }

        Image {
            id: deleteIcon
            width: deleteIconWidth
            height: deleteIconWidth
            sourceSize: Qt.size(deleteIconWidth, deleteIconWidth)
            visible: deleteIconVisible//modelData.uninstallable
            anchors {
                left: appIcon.right; leftMargin: deleteIconLeftMargin
                top: appIcon.top; topMargin: deleteIconTopMargin
            }
            source: deleteMouseArea.pressed ? deleteIconSourceOnPressed :
                                              deleteIconSource
            antialiasing: true
            MouseArea {
                id: deleteMouseArea
                anchors.fill: parent
                onClicked: {
                    deleteIconClicked()
                }
            }
        }

        ScaleAnimator {
            id: deleteiconAnimation;
            target: deleteIcon;
            from: 0;
            to: 1;
            duration: 300;
        }
        SequentialAnimation {
            id: appiconAnimation;
            loops: AnimatedSprite.Infinite;
            ParallelAnimation {
                RotationAnimation { target: iconBack; from: 0; to: 2; duration: 150; easing.type: Easing.OutCubic; }
            }
            ParallelAnimation {
                RotationAnimation { target: iconBack; from: 2; to: -2; duration: 150; easing.type: Easing.OutCubic; }
            }
        }

        Binding { target: selecticon; property: "selected"; value: root.selected }

        Rectangle {
            property bool selected: false

            id: selecticon;
            width: selecticonWidth
            height: selecticonWidth
            radius: selecticon.width/2
            color: "transparent"
            scale: 0
            anchors.left: appIcon.right
            anchors.leftMargin: (-selecticonWidth/2)
            anchors.top: appIcon.top
            anchors.topMargin: (-selecticonWidth/5)
            Image {
                id: selecticonImage
                width: parent.width
                height: parent.height
                source: selecticon.selected ? "qrc:/ui/images/ukui_choice_click" :
                                 "qrc:/ui/images/ukui_choice_default"
                anchors.centerIn: parent
            }
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    selecticon.selected = !selecticon.selected
                    modelManager.setItemSelectState(eduAppId, eduAppPageIndex, selecticon.selected)
                }
            }
        }
        ScaleAnimator {
            id: selecticonAnimation
            target: selecticon
            from: 0
            to: 1
            duration: 500
        }
    }

    Component.onCompleted: {
        if(iconState === LauncherItem.Selectable) {
            selecticonAnimation.start()
            deleteIcon.visible = false
        }
    }
}
